﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RouterControl.Whs.Wizards;
using Microsoft.HomeServer.Controls;

namespace RouterControl.Diag
{
    public partial class PageShowErrors : WizardPageClientBannerTop
    {
        private WizardMain ParentWizardMain
        {
            get { return (WizardMain)this.ParentWizard; }
        }

        public PageShowErrors()
        {
            InitializeComponent();
        }

        private void PageShowErrors_OkButtonClick(object sender, EventArgs e)
        {
            this.ParentWizard.Close();
        }

        public override void StartRealWork()
        {
            base.StartRealWork();

            // load errors
            this.errors.SuspendLayout();

            this.errors.Items.Clear();

            foreach (var err in this.ParentWizardMain.Errors)
            {
                var i = new MessageListBoxItem(err.MessageColor)
                {
                    Title = err.Title,
                    Icon = err.MessageIcon
                };

                // add description
                i.Description.Text = err.Text;

                // add url
                if (err.Link != null)
                {
                    var linkTxt = Properties.Resources.txtWizardErrorDetailsLinkText;

                    i.Description.Text += Environment.NewLine;

                    var start = i.Description.Text.Length;

                    i.Description.Text += linkTxt;

                    i.Description.LinkArea = new LinkArea(start, linkTxt.Length);

                    i.Description.Tag = err.Link;
                    i.Description.LinkClicked += new LinkLabelLinkClickedEventHandler(Description_LinkClicked);
                }

                this.errors.Items.Add(i);
            }

            this.errors.ResumeLayout();
        }

        private void Description_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var url = (Uri)((Control)sender).Tag;

            this.ParentWizardMain.Services.OpenUrl(url.ToString());
        }

    }
}
